package com.tanhua.server.controller;

import com.tanhua.common.utils.Cache;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("tanhua")
@Slf4j
public class TodayBestController {

    @Autowired
    private TodayBestService todayBestService;

    /**
     * 方法的请求头携带了Authorization参数,方法有返回值,可以使用我们定义的TodayBest对象封装
     * 查询今日佳人的接口
     *
     * @return
     */
    @GetMapping("todayBest")
    public ResponseEntity<TodayBest> findTodayBest() {
        try {
            //验证token
            TodayBest result = todayBestService.findTodayBest();
            if (result != null) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.error("查询今日佳人出错~ " + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 查询推荐用户列表的方法,方法的请求头携带了Authorization参数,还有Query作为分类查询的选项
     *
     * @param queryParam
     * @return
     */
    @GetMapping("recommendation")
    @Cache(time = "30")//开启缓存,设置缓存时间为30秒
    public ResponseEntity<PageResult> findPageInfo(RecommendUserQueryParam queryParam) {
        try {
            PageResult pageResult = todayBestService.findPageInfo(queryParam);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询推荐用户列表出错~ " + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
